package com.gx.smart.building.ui.warning.fragment

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter
import com.gx.smart.base.BaseMVVMRefreshFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentUntreatedBinding
import com.gx.smart.building.ui.warning.WarningDetailActivity
import com.gx.smart.building.ui.warning.adapter.WarningAdapter
import com.gx.smart.building.ui.warning.viewmodel.WarningViewModel
import com.gx.smart.lib.http.api.response.MessageAlarm
import com.scwang.smartrefresh.layout.api.RefreshLayout

class UntreatedFragment : BaseMVVMRefreshFragmentV2<WarningViewModel>(),
    BaseAdapter.OnItemClickListener<MessageAlarm> {
    private lateinit var viewBinding: FragmentUntreatedBinding
    private lateinit var adapter: WarningAdapter
    override fun onBindLayout(): Int = R.layout.fragment_untreated

    companion object {
        @JvmStatic
        fun newInstance() = UntreatedFragment()
    }

    override fun getToolbarTitle(): String? {
        return ""
    }

    override fun enableToolbar(): Boolean {
        return false
    }


    override fun onBindViewModel(): Class<WarningViewModel> = WarningViewModel::class.java

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        viewModel.query(this.currentPage, 1)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        viewModel.query(this.currentPage, 1)
    }

    override fun initContentView(root: ViewGroup?) {
        viewBinding = FragmentUntreatedBinding.inflate(layoutInflater, root, true)
        adapter = WarningAdapter(requireContext())
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun initData() {
        super.initData()
        viewBinding.refreshLayout.autoRefresh()
        viewModel.listUntreated.observe(viewLifecycleOwner) { list ->
            handleResult(list)
        }
    }

    override fun onItemClick(item: MessageAlarm, position: Int) {
        startActivity(Intent(requireContext(), WarningDetailActivity::class.java).apply {
            putExtra("id", item.id)
        })
    }

}